#ifdef HAVE_CONFIG_H
#        include <config.h>
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>

#include "support.h"
#include "newinfo.h"
#include "clist.h"
#include "file.h"
#include "addgroup.h"

#define GLADE_HOOKUP_OBJECT(component,widget,name) \
        g_object_set_data_full (G_OBJECT (component), name, \
                gtk_widget_ref (widget), (GDestroyNotify) gtk_widget_unref)

#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
        g_object_set_data (G_OBJECT (component), name, widget)

GtkWidget* 
create_new_info_window(struct newinfo_struct *newinfo,struct clist_struct *clist)
{
  GtkWidget *window;
  GtkWidget *label_name;
  GtkWidget *label_class;
  GtkWidget *label_tel;
  GtkWidget *label_mail;
  GtkWidget *label_blog;
  
  GtkWidget *label_help;
  GtkWidget *label_cancel;
  GtkWidget *label_ok;
    
  GtkWidget *entry_name;
  GtkWidget *entry_class;
  GtkWidget *entry_tel;
  GtkWidget *entry_mail;
  GtkWidget *entry_blog;
  
  GtkWidget *button_help;
  GtkWidget *button_cancel;
  GtkWidget *button_ok;
  
  GtkWidget *table_main;
  GtkWidget *alignment4;
  GtkWidget *hbox7;
  GtkWidget *image5;
  GtkWidget *hbox_tail;
  GtkWidget *fixed_new_info;
  GtkWidget *alignment5;
  GtkWidget *hbox8;
  GtkWidget *image6;
  GtkWidget *alignment6;
  GtkWidget *hbox9;
  GtkWidget *image7;

  
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_widget_set_size_request (window, 300, 251);
  gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
  gtk_window_set_modal (GTK_WINDOW (window), TRUE);
  gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
  gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DIALOG);

  table_main = gtk_table_new (6, 2, FALSE);
  gtk_widget_show (table_main);
  gtk_container_add (GTK_CONTAINER (window), table_main);
  gtk_widget_set_size_request (table_main, 267, 230);
  gtk_container_set_border_width (GTK_CONTAINER (table_main), 5);
  gtk_table_set_row_spacings (GTK_TABLE (table_main), 11);
  gtk_table_set_col_spacings (GTK_TABLE (table_main), 2);

  label_name = gtk_label_new (_("\345\247\223\345\220\215\357\274\232"));
  gtk_widget_show (label_name);
  gtk_table_attach (GTK_TABLE (table_main), label_name, 0, 1, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_widget_set_size_request (label_name, 50, 30);
  gtk_misc_set_alignment (GTK_MISC (label_name), 0, 0.5);
  gtk_misc_set_padding (GTK_MISC (label_name), 10, 0);
  gtk_label_set_width_chars (GTK_LABEL (label_name), 0);

  entry_name = gtk_entry_new ();
  gtk_widget_show (entry_name);
  gtk_table_attach (GTK_TABLE (table_main), entry_name, 1, 2, 0, 1,
                    (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_entry_set_invisible_char (GTK_ENTRY (entry_name), 9679);

  label_class = gtk_label_new (_("\347\217\255\347\272\247\357\274\232"));
  gtk_widget_show (label_class);
  gtk_table_attach (GTK_TABLE (table_main), label_class, 0, 1, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_widget_set_size_request (label_class, 50, 30);
  gtk_misc_set_alignment (GTK_MISC (label_class), 0, 0.5);
  gtk_misc_set_padding (GTK_MISC (label_class), 10, 0);

  label_tel = gtk_label_new (_("\347\224\265\350\257\235\357\274\232"));
  gtk_widget_show (label_tel);
  gtk_table_attach (GTK_TABLE (table_main), label_tel, 0, 1, 2, 3,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_widget_set_size_request (label_tel, 50, 30);
  gtk_misc_set_alignment (GTK_MISC (label_tel), 0, 0.5);
  gtk_misc_set_padding (GTK_MISC (label_tel), 10, 0);

  label_mail = gtk_label_new (_("\351\202\256\347\256\261\357\274\232"));
  gtk_widget_show (label_mail);
  gtk_table_attach (GTK_TABLE (table_main), label_mail, 0, 1, 3, 4,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_widget_set_size_request (label_mail, 50, 30);
  gtk_misc_set_alignment (GTK_MISC (label_mail), 0, 0.5);
  gtk_misc_set_padding (GTK_MISC (label_mail), 10, 0);

  label_blog = gtk_label_new (_("\345\215\232\345\256\242\357\274\232"));
  gtk_widget_show (label_blog);
  gtk_table_attach (GTK_TABLE (table_main), label_blog, 0, 1, 4, 5,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_widget_set_size_request (label_blog, 50, 30);
  gtk_misc_set_alignment (GTK_MISC (label_blog), 0, 0.5);
  gtk_misc_set_padding (GTK_MISC (label_blog), 10, 0);

  entry_class = gtk_entry_new ();
  gtk_widget_show (entry_class);
  gtk_table_attach (GTK_TABLE (table_main), entry_class, 1, 2, 1, 2,
                    (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_entry_set_invisible_char (GTK_ENTRY (entry_class), 9679);

  entry_tel = gtk_entry_new ();
  gtk_widget_show (entry_tel);
  gtk_table_attach (GTK_TABLE (table_main), entry_tel, 1, 2, 2, 3,
                    (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_entry_set_invisible_char (GTK_ENTRY (entry_tel), 9679);

  entry_mail = gtk_entry_new ();
  gtk_widget_show (entry_mail);
  gtk_table_attach (GTK_TABLE (table_main), entry_mail, 1, 2, 3, 4,
                    (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_entry_set_invisible_char (GTK_ENTRY (entry_mail), 9679);

  entry_blog = gtk_entry_new ();
  gtk_widget_show (entry_blog);
  gtk_table_attach (GTK_TABLE (table_main), entry_blog, 1, 2, 4, 5,
                    (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_entry_set_invisible_char (GTK_ENTRY (entry_blog), 9679);

  button_help = gtk_button_new ();
  gtk_widget_show (button_help);
  gtk_table_attach (GTK_TABLE (table_main), button_help, 0, 1, 5, 6,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_widget_set_size_request (button_help, 80, 35);

  alignment4 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment4);
  gtk_container_add (GTK_CONTAINER (button_help), alignment4);

  hbox7 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox7);
  gtk_container_add (GTK_CONTAINER (alignment4), hbox7);

  image5 = gtk_image_new_from_stock ("gtk-help", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image5);
  gtk_box_pack_start (GTK_BOX (hbox7), image5, FALSE, FALSE, 0);

  label_help = gtk_label_new_with_mnemonic ("\345\270\256\345\212\251");
  gtk_widget_show (label_help);
  gtk_box_pack_start (GTK_BOX (hbox7), label_help, FALSE, FALSE, 0);

  hbox_tail = gtk_hbox_new (FALSE, 0);
  gtk_widget_show (hbox_tail);
  gtk_table_attach (GTK_TABLE (table_main), hbox_tail, 1, 2, 5, 6,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (GTK_FILL), 0, 0);

  fixed_new_info = gtk_fixed_new ();
  gtk_widget_show (fixed_new_info);
  gtk_box_pack_start (GTK_BOX (hbox_tail), fixed_new_info, TRUE, TRUE, 0);

  button_cancel = gtk_button_new ();
  gtk_widget_show (button_cancel);
  gtk_box_pack_start (GTK_BOX (hbox_tail), button_cancel, FALSE, FALSE, 0);
  gtk_widget_set_size_request (button_cancel, 90, 35);

  alignment5 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment5);
  gtk_container_add (GTK_CONTAINER (button_cancel), alignment5);

  hbox8 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox8);
  gtk_container_add (GTK_CONTAINER (alignment5), hbox8);

  image6 = gtk_image_new_from_stock ("gtk-cancel", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image6);
  gtk_box_pack_start (GTK_BOX (hbox8), image6, FALSE, FALSE, 0);

  label_cancel = gtk_label_new_with_mnemonic ("\345\217\226\346\266\210");
  gtk_widget_show (label_cancel);
  gtk_box_pack_start (GTK_BOX (hbox8), label_cancel, FALSE, FALSE, 0);

  button_ok = gtk_button_new ();
  gtk_widget_show (button_ok);
  gtk_box_pack_start (GTK_BOX (hbox_tail), button_ok, FALSE, FALSE, 0);
  gtk_widget_set_size_request (button_ok, 90, 35);

  alignment6 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment6);
  gtk_container_add (GTK_CONTAINER (button_ok), alignment6);

  hbox9 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox9);
  gtk_container_add (GTK_CONTAINER (alignment6), hbox9);

  image7 = gtk_image_new_from_stock ("gtk-ok", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image7);
  gtk_box_pack_start (GTK_BOX (hbox9), image7, FALSE, FALSE, 0);

  label_ok = gtk_label_new_with_mnemonic ("\347\241\256\345\256\232");
  gtk_widget_show (label_ok);
  gtk_box_pack_start (GTK_BOX (hbox9), label_ok, FALSE, FALSE, 0);



  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (window, window, "window");
  GLADE_HOOKUP_OBJECT (window, table_main, "table_main");
  GLADE_HOOKUP_OBJECT (window, label_name, "label_name");
  GLADE_HOOKUP_OBJECT (window, entry_name, "entry_name");
  GLADE_HOOKUP_OBJECT (window, label_class, "label_class");
  GLADE_HOOKUP_OBJECT (window, label_tel, "label_tel");
  GLADE_HOOKUP_OBJECT (window, label_mail, "label_mail");
  GLADE_HOOKUP_OBJECT (window, label_blog, "label_blog");
  GLADE_HOOKUP_OBJECT (window, entry_class, "entry_class");
  GLADE_HOOKUP_OBJECT (window, entry_tel, "entry_tel");
  GLADE_HOOKUP_OBJECT (window, entry_mail, "entry_mail");
  GLADE_HOOKUP_OBJECT (window, entry_blog, "entry_blog");
  GLADE_HOOKUP_OBJECT (window, button_help, "button_help");
  GLADE_HOOKUP_OBJECT (window, alignment4, "alignment4");
  GLADE_HOOKUP_OBJECT (window, hbox7, "hbox7");
  GLADE_HOOKUP_OBJECT (window, image5, "image5");
  GLADE_HOOKUP_OBJECT (window, label_help, "label_help");
  GLADE_HOOKUP_OBJECT (window, hbox_tail, "hbox_tail");
  GLADE_HOOKUP_OBJECT (window, fixed_new_info, "fixed_new_info");
  GLADE_HOOKUP_OBJECT (window, button_cancel, "button_cancel");
  GLADE_HOOKUP_OBJECT (window, alignment5, "alignment5");
  GLADE_HOOKUP_OBJECT (window, hbox8, "hbox8");
  GLADE_HOOKUP_OBJECT (window, image6, "image6");
  GLADE_HOOKUP_OBJECT (window, label_cancel, "label_cancel");
  GLADE_HOOKUP_OBJECT (window, button_ok, "button_ok");
  GLADE_HOOKUP_OBJECT (window, alignment6, "alignment6");
  GLADE_HOOKUP_OBJECT (window, hbox9, "hbox9");
  GLADE_HOOKUP_OBJECT (window, image7, "image7");
  GLADE_HOOKUP_OBJECT (window, label_ok, "label_ok");

  gtk_widget_grab_focus (entry_name);
  
    /*init newininfo struct*/
  newinfo->window = window;
	 
  newinfo->label_name = label_name ;
  newinfo->label_class =  label_class;
  newinfo->label_tel =  label_tel;
  newinfo->label_mail =  label_mail;
  newinfo->label_blog = label_blog ; 
  
  newinfo->label_help =  label_help;
  newinfo->label_cancel = label_cancel ;
  newinfo->label_ok =  label_ok;  
    
  newinfo->entry_name =  entry_name;
  newinfo->entry_class =  entry_class;
  newinfo->entry_tel =  entry_tel;
  newinfo->entry_mail =  entry_mail;
  newinfo->entry_blog = entry_blog ;
  
  newinfo->button_help=button_help;
  newinfo->button_cancel=button_cancel;
  newinfo->button_ok=button_ok;
  
  return window;
}

GtkWidget*
create_new_add_window (struct addgroup_struct *addgroup,struct clist_struct *clist)
{
  GtkWidget *window;
  
  GtkWidget *label_cancel;
  GtkWidget *label_ok;
  GtkWidget *label_help; 
  GtkWidget *label_name;
  
  GtkWidget *entry_name;
 
  GtkWidget *button_help;
  GtkWidget *button_cancel; 
  GtkWidget *button_ok;
  GtkWidget *table_main;
  GtkWidget *hbox_main;

  GtkWidget *alignment1;
  GtkWidget *hbox4;
  GtkWidget *image2;
  GtkWidget *alignment2;
  GtkWidget *hbox5;
  GtkWidget *image3;
  GtkWidget *alignment3;
  GtkWidget *hbox6;
  GtkWidget *image4;


  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
  gtk_window_set_modal (GTK_WINDOW (window), TRUE);
  gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
  gtk_window_set_type_hint (GTK_WINDOW (window), GDK_WINDOW_TYPE_HINT_DIALOG);

  table_main = gtk_table_new (2, 2, FALSE);
  gtk_widget_show (table_main);
  gtk_container_add (GTK_CONTAINER (window), table_main);
  gtk_container_set_border_width (GTK_CONTAINER (table_main), 8);
  gtk_table_set_row_spacings (GTK_TABLE (table_main), 6);
  gtk_table_set_col_spacings (GTK_TABLE (table_main), 14);

  hbox_main = gtk_hbox_new (FALSE, 0);
  gtk_widget_show (hbox_main);
  gtk_table_attach (GTK_TABLE (table_main), hbox_main, 1, 2, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0);

  button_cancel = gtk_button_new ();
  gtk_widget_show (button_cancel);
  gtk_box_pack_start (GTK_BOX (hbox_main), button_cancel, FALSE, FALSE, 0);

  alignment1 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment1);
  gtk_container_add (GTK_CONTAINER (button_cancel), alignment1);

  hbox4 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox4);
  gtk_container_add (GTK_CONTAINER (alignment1), hbox4);

  image2 = gtk_image_new_from_stock ("gtk-cancel", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image2);
  gtk_box_pack_start (GTK_BOX (hbox4), image2, FALSE, FALSE, 0);

  label_cancel = gtk_label_new_with_mnemonic (_(" \345\217\226\346\266\210 "));
  gtk_widget_show (label_cancel);
  gtk_box_pack_start (GTK_BOX (hbox4), label_cancel, FALSE, FALSE, 0);
  gtk_misc_set_padding (GTK_MISC (label_cancel), 3, 0);

  button_ok = gtk_button_new ();
  gtk_widget_show (button_ok);
  gtk_box_pack_start (GTK_BOX (hbox_main), button_ok, FALSE, FALSE, 0);

  alignment2 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment2);
  gtk_container_add (GTK_CONTAINER (button_ok), alignment2);

  hbox5 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox5);
  gtk_container_add (GTK_CONTAINER (alignment2), hbox5);

  image3 = gtk_image_new_from_stock ("gtk-ok", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image3);
  gtk_box_pack_start (GTK_BOX (hbox5), image3, FALSE, FALSE, 0);
  gtk_misc_set_padding (GTK_MISC (image3), 3, 0);

  label_ok = gtk_label_new_with_mnemonic (_(" \347\241\256\345\256\232 "));
  gtk_widget_show (label_ok);
  gtk_box_pack_start (GTK_BOX (hbox5), label_ok, FALSE, FALSE, 0);

  label_name = gtk_label_new (_("\350\276\223\345\205\245\347\273\204\345\220\215\357\274\232"));
  gtk_widget_show (label_name);
  gtk_table_attach (GTK_TABLE (table_main), label_name, 0, 1, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_misc_set_alignment (GTK_MISC (label_name), 0, 0.5);

  entry_name = gtk_entry_new ();
  gtk_widget_show (entry_name);
  gtk_table_attach (GTK_TABLE (table_main), entry_name, 1, 2, 0, 1,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);
  gtk_widget_set_size_request (entry_name, 150, -1);
  gtk_entry_set_invisible_char (GTK_ENTRY (entry_name), 9679);

  button_help = gtk_button_new ();
  gtk_widget_show (button_help);
  gtk_table_attach (GTK_TABLE (table_main), button_help, 0, 1, 1, 2,
                    (GtkAttachOptions) (GTK_FILL),
                    (GtkAttachOptions) (0), 0, 0);

  alignment3 = gtk_alignment_new (0.5, 0.5, 0, 0);
  gtk_widget_show (alignment3);
  gtk_container_add (GTK_CONTAINER (button_help), alignment3);

  hbox6 = gtk_hbox_new (FALSE, 2);
  gtk_widget_show (hbox6);
  gtk_container_add (GTK_CONTAINER (alignment3), hbox6);

  image4 = gtk_image_new_from_stock ("gtk-help", GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image4);
  gtk_box_pack_start (GTK_BOX (hbox6), image4, FALSE, FALSE, 0);

  label_help = gtk_label_new_with_mnemonic (_(" \345\270\256\345\212\251 "));
  gtk_widget_show (label_help);
  gtk_box_pack_start (GTK_BOX (hbox6), label_help, FALSE, FALSE, 0);

  /* Store pointers to all widgets, for use by lookup_widget(). */
  GLADE_HOOKUP_OBJECT_NO_REF (window, window, "window");
  GLADE_HOOKUP_OBJECT (window, table_main, "table_main");
  GLADE_HOOKUP_OBJECT (window, hbox_main, "hbox_main");
  GLADE_HOOKUP_OBJECT (window, button_cancel, "button_cancel");
  GLADE_HOOKUP_OBJECT (window, alignment1, "alignment1");
  GLADE_HOOKUP_OBJECT (window, hbox4, "hbox4");
  GLADE_HOOKUP_OBJECT (window, image2, "image2");
  GLADE_HOOKUP_OBJECT (window, label_cancel, "label_cancel");
  GLADE_HOOKUP_OBJECT (window, button_ok, "button_ok");
  GLADE_HOOKUP_OBJECT (window, alignment2, "alignment2");
  GLADE_HOOKUP_OBJECT (window, hbox5, "hbox5");
  GLADE_HOOKUP_OBJECT (window, image3, "image3");
  GLADE_HOOKUP_OBJECT (window, label_ok, "label_ok");
  GLADE_HOOKUP_OBJECT (window, label_name, "label_name");
  GLADE_HOOKUP_OBJECT (window, entry_name, "entry_name");
  GLADE_HOOKUP_OBJECT (window, button_help, "button_help");
  GLADE_HOOKUP_OBJECT (window, alignment3, "alignment3");
  GLADE_HOOKUP_OBJECT (window, hbox6, "hbox6");
  GLADE_HOOKUP_OBJECT (window, image4, "image4");
  GLADE_HOOKUP_OBJECT (window, label_help, "label_help");

  addgroup->clist=clist;
  addgroup->label_cancel=label_cancel;
  addgroup->label_ok=label_ok;
  addgroup->label_help=label_help; 
  addgroup->label_name=label_name;
  addgroup->entry_name=entry_name;
  addgroup->button_cancel=button_cancel;
  addgroup->button_help=button_help;
  addgroup->button_ok=button_ok;
  
  return window;
}
